// pages/receive/editCcontract/editCcontract.js
import api from '../../../utils/api';
import util from '../../../utils/util';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username:'HTBH2020072721',
    canEdit:false,
    contractData:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.claimContractId = options.claimContractId;
    this.setData({
      canEdit:options.canEdit==1?true:false
    })
    this.getData()
  },
  getData(){
    let that = this;
    api.getClaimContractById({
      data:{
        claimContractId:that.data.claimContractId
      },
      success:function(res){
        res.data.data[0].contractMoney = util.formatFigure(res.data.data[0].contractMoney);
        res.data.data[0].claimedMoney = util.formatFigure(res.data.data[0].claimedMoney);
        res.data.data[0].invContMoney = util.formatFigure(res.data.data[0].invContMoney);
        res.data.data[0].remainingMoney = util.formatFigure(res.data.data[0].remainingMoney);
        res.data.data[0].claimMoney = that.data.canEdit ? res.data.data[0].claimMoney : util.formatFigure(res.data.data[0].claimMoney);
        that.getBasicData(res.data.data[0].claimId)
        that.setData({
          contractData:res.data.data[0]
        })
      }
    })
  },
  // 基本信息
  getBasicData(claimId){
    let that = this;
    api.getClaimById({
      data:{
        claimId:claimId
      },
      success:function(res){
        console.log(res)
        if(res.data.code == 200){
          that.setData({
            basicData:res.data.data[0]
          })
        }
      }
    })
  },
  onChange(e){
    this.data.contractData.claimMoney = parseFloat(e.detail.replace("¥","").replace(/\s*/g,''));
  },
  // 备注
  onRemarksChange(e) {
    console.log(e)
    this.data.contractData.remarks = e.detail
  },
  // 保存
  onSave(){
    let that = this;
    console.log(that.data.contractData.claimMoney)
    api.editClaimContract({
      data:{
        id:that.data.contractData.id,
        money:that.data.contractData.claimMoney,
        remarks:that.data.contractData.remarks
      },
      method:'POST',
      success:function(res){
        console.log(res)
        if(res.data.code==200){
          wx.showToast({
            title: '编辑成功',
          })
          setTimeout(() => {
            wx.navigateBack({
              delta: 1,
            })
          }, 100);
        }else{
          wx.showModal({
            content:res.data.message,
            showCancel:false,
            confirmColor:'#0041C4'
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})